# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=tk9
pkgver=9.0.0
pkgrel=0
_major=${pkgver%.*}
pkgdesc="GUI toolkit for the Tcl scripting language, version 9"
url="https://www.tcl-lang.org/"
arch="all"
license="TCL"
depends_dev="
	fontconfig-dev
	libx11-dev
	libxft-dev
	tcl9-dev~$pkgver
	"
makedepends="
	$depends_dev
	libpng-dev
	"
checkdepends="xvfb-run"
subpackages="$pkgname-dev $pkgname-doc"
source="https://downloads.sourceforge.net/sourceforge/tcl/tk$pkgver-src.tar.gz"
builddir="$srcdir/tk$pkgver/unix"

prepare() {
	local builddir="$srcdir/tk$pkgver"

	default_prepare
}

build() {
	case "$CARCH" in
		arm*|x86)	_64bit="--disable-64bit" ;;
		*)		_64bit="--enable-64bit"  ;;
	esac

	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var \
		--disable-rpath \
		--disable-zipfs \
		$_64bit
	make
}

check() {
	xvfb-run -a make test TESTFLAGS="-verbose pbtels"
}

package() {
	make INSTALL_ROOT="$pkgdir" install install-private-headers

	ln -sf wish$_major "$pkgdir"/usr/bin/wish9
	install -Dm644 ../license.terms \
		"$pkgdir"/usr/share/licenses/$pkgname/LICENSE

	# remove buildroot traces
	find "$pkgdir" -name '*Config.sh' -print0 |
		xargs -0 sed -i -e "s#${srcdir}#/usr/src#"

	# move demos to -doc directory
	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/examples
	mv -v "$pkgdir"/usr/lib/tk$_major/demos \
		"$pkgdir"/usr/share/doc/$pkgname/examples/
}

dev() {
	# libtcl9tk9.0.so is the only library made and it remains in the mainpackage
	# so depending on the main package automatically is never done
	# https://gitlab.alpinelinux.org/alpine/aports/issues/11232#note_68789
	depends_dev="$depends_dev $pkgname=$pkgver-r$pkgrel"
	default_dev

	local file; find "$pkgdir" -name '*Config.sh' | while read -r file; do
		amove "${file#"$pkgdir"}"
	done
}

sha512sums="
0e97124d58e3d756e41c2bc8ee3e4a7e6633e0c42b441bd21960734f8b46ce07ed496386c06bbf3281d92234feac0178dcf846e8b098d5b7bedcfbd471b90af5  tk9.0.0-src.tar.gz
"
